package com.cyh.testcustomui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class TagLayout extends ViewGroup {

    private List<List<View>> mChildViews = new ArrayList<>();
    public TagLayout(Context context) {
        this(context, null);
    }

    public TagLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TagLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mChildViews.clear();

        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = getPaddingTop() + getPaddingBottom();  //后面通过这个高度加上每一行的高度

        int lineWidth = getPaddingLeft() + getPaddingRight(); //一行的宽度


        ArrayList<View> childViews = new ArrayList<>();

        mChildViews.add(childViews);

        int childCount = getChildCount();
        int maxFirstHeight = 0;
        for(int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            //这段代码之后就可以获取子View的宽高，因为会调用子View的onMeasure方法。
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);

            //获取margin的值, ViewGroup.LayoutParams 没有margin值
            //为什么LinearLayout.LayoutParams有margin值? 因为LinearLayout有自己的LayoutParams
            //ViewGroup.LayoutParams params = childView.getLayoutParams(); //这个没有margin, 需要强转, 覆写generateLayoutParams方法
            ViewGroup.MarginLayoutParams params = (MarginLayoutParams) childView.getLayoutParams();

            //还需要考虑margin
            if(lineWidth + (childView.getMeasuredWidth() + params.leftMargin + params.rightMargin) > width) {
                height += childView.getMeasuredHeight() + params.topMargin + params.bottomMargin;
                lineWidth = getPaddingLeft() + getPaddingRight();

                childViews = new ArrayList<>();
                mChildViews.add(childViews);
            } else {
                lineWidth += childView.getMeasuredWidth()  + params.leftMargin + params.rightMargin;

                childViews.add(childView);

                maxFirstHeight = Math.max(childView.getMeasuredHeight() + params.topMargin + params.bottomMargin, maxFirstHeight);
            }
        }

        height += maxFirstHeight;

        //计算自己的宽高
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        int childCount = getChildCount();
        int left=0, top = getPaddingTop(), right=0, bottom=0;

        for(List<View> childViews : mChildViews) {
            left = getPaddingLeft();

            for(View childView : childViews) {
                ViewGroup.MarginLayoutParams params = (MarginLayoutParams) childView.getLayoutParams();
                left += params.leftMargin;

                right = left + childView.getMeasuredWidth();
                bottom = top + childView.getMeasuredHeight();
                //摆放
                childView.layout(left, top, right, bottom);
                //left叠加
                left += childView.getMeasuredWidth();
            }

            top += childViews.get(0).getMeasuredHeight();

        }

    }


    //自定义的ViewGroup需要获得margin, 必须覆写这个方法，否则不能强转获得margin
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        //return super.generateLayoutParams(attrs);
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return super.generateLayoutParams(p);
    }
}
